package com.wgo.precise.client.ui.view.editor.usertypes;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IDetailsPage;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.IManagedForm;

import com.wgo.precise.client.ui.controller.listeners.IModelUpdateListener;
import com.wgo.precise.client.ui.model.PropertyDefWrapper;
import com.wgo.precise.client.ui.view.editor.properties.PropertiesSectionPart;


/**
 * 
 * @author petterei
 *
 * @version $Id: PropertyDefinitionDetailsPage.java,v 1.2 2006-01-30 21:52:32 petterei Exp $
 */
class PropertyDefinitionDetailsPage implements IDetailsPage {

	private PropertiesSectionPart part = null;
	private IManagedForm form = null;
	private PropertyDefWrapper wrapper = null;
	private IModelUpdateListener listener = null;
	/**
	 * @see org.eclipse.ui.forms.IFormPart#initialize(org.eclipse.ui.forms.IManagedForm)
	 */
	public void initialize(IManagedForm newForm) {
		this.form = newForm; 
		this.listener = getMasterPart();
	}
	private IModelUpdateListener getMasterPart() {
		for (IFormPart formParts : form.getParts()) {
			if (formParts instanceof UsertypesSectionPart) {
				return (UsertypesSectionPart) formParts ;
			}
		}
		return null;
	}
	/**
	 * @see org.eclipse.ui.forms.IDetailsPage#createContents(org.eclipse.swt.widgets.Composite)
	 */
	public void createContents(Composite parent) {
		FillLayout layout = new FillLayout(SWT.VERTICAL);
		layout.marginWidth = 1;
		layout.marginHeight = 1;
		parent.setLayout(layout);
		part = new PropertiesSectionPart(form.getToolkit(), parent);
		part.initialize(form);
	}
	/**
	 * @see org.eclipse.ui.forms.IFormPart#setFormInput(java.lang.Object)
	 */
	public boolean setFormInput(Object input) {
		if (wrapper != null && listener != null) {
			wrapper.removeChangeListener(listener);
			wrapper = null;
		}
		boolean value = part.setFormInput(input);
		if (input instanceof PropertyDefWrapper && listener != null) {
			wrapper = (PropertyDefWrapper)input;
			wrapper.addChangeListener(listener);
		}
		part.setFocus();
		return value;
	}

	/**
	 * @see org.eclipse.ui.forms.IPartSelectionListener#selectionChanged(org.eclipse.ui.forms.IFormPart, org.eclipse.jface.viewers.ISelection)
	 */
	public void selectionChanged(@SuppressWarnings("unused") IFormPart targetPart, ISelection selection) {
		IStructuredSelection structuredSelection = (IStructuredSelection)selection;
		setFormInput(structuredSelection.getFirstElement());
	}

	/**
	 * @see org.eclipse.ui.forms.IFormPart#commit(boolean)
	 */
	public void commit(boolean onSave) {
		part.commit(onSave);
	}

	/**
	 * @see org.eclipse.ui.forms.IFormPart#dispose()
	 */
	public void dispose() {
		if (null != listener) {
			if (null != wrapper){
				wrapper.removeChangeListener(listener);
			}
			listener = null;
		}
		wrapper =null;
		form = null;
		if (null != part) {
			part.dispose();
		}
		part = null;
	}

	/**
	 * @see org.eclipse.ui.forms.IFormPart#isDirty()
	 */
	public boolean isDirty() {
		return part.isDirty();
	}

	/**
	 * @see org.eclipse.ui.forms.IFormPart#isStale()
	 */
	public boolean isStale() {
		return part.isStale();
	}

	/**
	 * @see org.eclipse.ui.forms.IFormPart#refresh()
	 */
	public void refresh() {
		part.refresh();
	}

	/**
	 * @see org.eclipse.ui.forms.IFormPart#setFocus()
	 */
	public void setFocus() {
		part.setFocus();
	}

}